<template>
  <div class="Reg-container">
    <div class="card">
      <div class="card-header"><span>云端中控</span></div>
      <div class="card-main">
        <span class="span_inputname">账号</span>
        <el-input class="mb-20" ref="username" v-model="RegForm.username" placeholder="账号" name="username" type="text"
          tabindex="1" style="width: 100%;" autocomplete="off" />

        <!-- 添加虚拟字段来混淆自动填充 -->
        <input type="text" style="display:none;" />
        <input type="password" style="display:none;" />

        <span class="span_inputname">密码</span>
        <el-input :key="passwordKey1" ref="password" v-model="RegForm.password" :type="passwordType" placeholder="密码"
          name="new-password" @keyup.enter.native="handleReg" style="width: 100%;" class="mb-20"
          autocomplete="new-password">
          <span class="show-pwd" @click="togglePasswordVisibility('password')" slot="suffix">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-input>

        <span class="span_inputname">确认密码</span>
        <el-input :key="passwordKey2" ref="password2" v-model="RegForm.password2" :type="passwordType2" placeholder="确认密码"
          name="new-password" @keyup.enter.native="checkPwd" style="width: 100%;" class="mb-20"
          autocomplete="new-password">
          <span class="show-pwd" @click="togglePasswordVisibility('password2')" slot="suffix">
            <svg-icon :icon-class="passwordType2 === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-input>

        <div class="flex justify-between align-center">
          <el-button :loading="loading" type="primary" @click.native.prevent="handleReg"
            style="min-width: 120px;">注册</el-button>
        </div>

        <div class="card_foot">
          <span>已有账号?</span>
          <el-link type="primary" @click="toLog">点击这里登录</el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { doSigin } from '@/api/user'
export default {
  name: 'Reg',
  data() {
    return {
      RegForm: {
        username: '',
        password: '',
        password2: '',
        code:''
      },
      loading: false,
      passwordType: 'password',
      passwordType2: 'password',
      passwordKey1: this.generateUniqueKey(), // 唯一标识符
      passwordKey2: this.generateUniqueKey(), // 唯一标识符
    }
  },

  methods: {
    toLog() {
      this.$router.push('/login');
    },
    generateUniqueKey() {
      return Date.now() + Math.random().toString(36).substr(2, 9);
    },

    togglePasswordVisibility(field) {
      if (field === 'password') {
        this.passwordType = this.passwordType === 'password' ? 'text' : 'password';
        this.passwordKey1 = this.generateUniqueKey(); // 触发重新渲染
      } else if (field === 'password2') {
        this.passwordType2 = this.passwordType2 === 'password' ? 'text' : 'password';
        this.passwordKey2 = this.generateUniqueKey(); // 触发重新渲染
      }
      this.$nextTick(() => {
        if (field === 'password') {
          this.$refs.password.focus();
        } else if (field === 'password2') {
          this.$refs.password2.focus();
        }
      });
    },
    checkPwd() {
      if (this.RegForm.password !== this.RegForm.password2) {
        this.$message({
          message: '两次密码不一致',
          type: 'warning'
        });
        return;
      }
    },
    handleReg() {
      if (!this.RegForm.username) {
        this.$message.error('请输入账号');
        return;
      }
      if (!this.RegForm.password) {
        this.$message.error('请输入密码');
        return;
      }
      if (this.RegForm.password !== this.RegForm.password2) {
        this.$message({
          message: '两次密码不一致',
          type: 'warning'
        });
        return;
      }
      this.loading = true;
      doSigin(this.RegForm).then((res) => {
        this.loading = false;
        if (res.Code === 200) {
          this.$message.success(res.Msg);
          setTimeout(() => {
            this.$router.push('/login');
          }, 1000);
        }
      }).catch((err) => {
        this.loading = false;
      });
    },
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .Reg-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.Reg-container {
  background-image: url('../../assets/images/登录背景.png');
  background-repeat: no-repeat;
  background-position-x: 50%;
  background-position-y: 50%;
  background-size: 100%;
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      // color: $light_gray;
      // caret-color: $cursor;
      font-family: Arial, Helvetica, sans-serif;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #ffff inset !important;
        // -webkit-text-fill-color: $cursor !important;
      }
    }
  }
}

.show-pwd {
  height: 100%;
  position: absolute;
  right: 5px;
  top: 10px;
  font-size: 16px;
  color: #889aa4;
  cursor: pointer;
  user-select: none;
}

.card {
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 2px 12px 0px;
  box-sizing: border-box;
  color: rgb(68, 68, 68);
  width: 700px;
  max-width: 700px;
  transform: translate(-50%, -50%);
  position: absolute;
  top: 40%;
  left: 50%;
  background-image: url('../../assets/images/云端管理.png');
  background-repeat: no-repeat;
  background-origin: padding-box;
  padding: 20px 20px 20px 40px;
  background-position: 93% 60%;

  .card-header {
    box-sizing: border-box;
    height: 80px;
    line-height: 90px;
    font-weight: bold;
    font-size: 20px;
  }

  .card-main {
    box-sizing: border-box;
    max-width: 300px;
    color: #444;
    padding-top: 20px;
  }

  .card_foot {
    height: 60px;
    margin-top: 20px;
    border-top: 1px solid #dcdfe6;
    color: #888;
    font-size: 14px;

    span {
      display: inline-block;
      margin-top: 30px;
      margin-right: 10px;
    }
  }
}

.span_inputname {
  margin-bottom: 10px;
  font-size: 14px;
  color: #444;
  display: inline-block;
}
.el-link{
  vertical-align: bottom;
}

// 手机端适配
@media only screen and (max-width: 840px) {
  body {
    background-image: none;
  }
}

@media only screen and (max-width: 840px) {
  .card {
    box-shadow: none;
    box-sizing: border-box;
    padding: 0;
    color: #444;
    width: 100%;
    max-width: 100%;
    -webkit-transform: none;
    -moz-transform: none;
    -ms-transform: none;
    -o-transform: none;
    transform: none;
    position: absolute;
    top: 0;
    left: 0;
    padding-right: 20px;
    background-image: none;
    background-repeat: no-repeat;
    /* background-size: 100%; */
    background-position-x: right;
    background-position-y: center;
    background-origin: padding-box;
    background-position: 93% 60%;
  }
}

@media only screen and (max-width: 840px) {
  .card-header {
    box-sizing: border-box;
    height: 250px !important;
    width: 100%;
    text-align: center;
    background-image: url('../../assets/images/云端管理.png');
    background-repeat: no-repeat;
    background-size: 250px;
    background-position: 60% 50%;
    margin-top: 20px;
  }
}

@media only screen and (max-width: 840px) {
  .card-main {
    width: 100% !important;
    margin: 0;
    max-width: 100% !important;
    padding-left: 20px;
    padding-top: 20px;
  }
}

@media only screen and (max-width: 840px) {
  .card-header>span {
    display: none;
  }
}
</style>